@@ -24,7 +24,7 @@ class WebRequestsController < ApplicationController |
||
24 | 24 |
if user |
25 | 25 |
agent = user.agents.find_by_id(params[:agent_id]) |
26 | 26 |
if agent |
27 |
- content, status, content_type = agent.trigger_web_request(params.except(:action, :controller, :agent_id, :user_id, :format), request.method_symbol.to_s, request.format.to_s, request.headers) |
|
27 |
+ content, status, content_type = agent.trigger_web_request(params.except(:action, :controller, :agent_id, :user_id, :format), request.method_symbol.to_s, request.format.to_s, request.headers, request) |
|
28 | 28 |
|
29 | 29 |
if content.is_a?(String) |
30 | 30 |
render :text => content, :status => status || 200, :content_type => content_type || 'text/plain' |
@@ -47,7 +47,7 @@ class WebRequestsController < ApplicationController |
||
47 | 47 |
secret = params[:secret] |
48 | 48 |
user.agents.of_type(Agents::UserLocationAgent).each { |agent| |
49 | 49 |
if agent.options[:secret] == secret |
50 |
- agent.trigger_web_request(params.except(:action, :controller, :user_id, :format), request.method_symbol.to_s, request.format.to_s, request.headers) |
|
50 |
+ agent.trigger_web_request(params.except(:action, :controller, :user_id, :format), request.method_symbol.to_s, request.format.to_s, request.headers, request) |
|
51 | 51 |
end |
52 | 52 |
} |
53 | 53 |
render :text => "ok" |
@@ -95,7 +95,7 @@ class Agent < ActiveRecord::Base |
||
95 | 95 |
false |
96 | 96 |
end |
97 | 97 |
|
98 |
- def receive_web_request(params, method, format, headers={}) |
|
98 |
+ def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({})) |
|
99 | 99 |
# Implement me in your subclass of Agent. |
100 | 100 |
["not implemented", 404] |
101 | 101 |
end |
@@ -149,20 +149,20 @@ class Agent < ActiveRecord::Base |
||
149 | 149 |
end |
150 | 150 |
end |
151 | 151 |
|
152 |
- def trigger_web_request(params, method, format, headers) |
|
152 |
+ def trigger_web_request(params, method, format, headers, request) |
|
153 | 153 |
if respond_to?(:receive_webhook) |
154 | 154 |
Rails.logger.warn "DEPRECATED: The .receive_webhook method is deprecated, please switch your Agent to use .receive_web_request." |
155 | 155 |
receive_webhook(params).tap do |
156 | 156 |
self.last_web_request_at = Time.now |
157 | 157 |
save! |
158 | 158 |
end |
159 |
- elsif method(:receive_web_request).arity == 3 |
|
160 |
- receive_web_request(params, method, format).tap do |
|
161 |
- self.last_web_request_at = Time.now |
|
162 |
- save! |
|
163 |
- end |
|
164 | 159 |
else |
165 |
- receive_web_request(params, method, format, headers).tap do |
|
160 |
+ if method(:receive_web_request).arity == 3 |
|
161 |
+ handled_request = receive_web_request(params, method, format) |
|
162 |
+ else |
|
163 |
+ handled_request = receive_web_request(params, method, format, headers, request) |
|
164 |
+ end |
|
165 |
+ handled_request.tap do |
|
166 | 166 |
self.last_web_request_at = Time.now |
167 | 167 |
save! |
168 | 168 |
end |
@@ -160,7 +160,7 @@ module Agents |
||
160 | 160 |
interpolated['push_hubs'].presence || [] |
161 | 161 |
end |
162 | 162 |
|
163 |
- def receive_web_request(params, method, format, headers={}) |
|
163 |
+ def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({})) |
|
164 | 164 |
unless interpolated['secrets'].include?(params['secret']) |
165 | 165 |
if format =~ /json/ |
166 | 166 |
return [{ error: "Not Authorized" }, 401] |
@@ -81,7 +81,7 @@ module Agents |
||
81 | 81 |
"#{server_url}/users/#{user.id}/web_requests/#{id}/#{secret}" |
82 | 82 |
end |
83 | 83 |
|
84 |
- def receive_web_request(params, method, format, headers={}) |
|
84 |
+ def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({})) |
|
85 | 85 |
if memory['pending_calls'].has_key? params['secret'] |
86 | 86 |
response = Twilio::TwiML::Response.new {|r| r.Say memory['pending_calls'][params['secret']], :voice => 'woman'} |
87 | 87 |
memory['pending_calls'].delete params['secret'] |
@@ -57,7 +57,7 @@ module Agents |
||
57 | 57 |
end |
58 | 58 |
end |
59 | 59 |
|
60 |
- def receive_web_request(params, method, format, headers={}) |
|
60 |
+ def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({})) |
|
61 | 61 |
params = params.symbolize_keys |
62 | 62 |
if method != 'post' |
63 | 63 |
return ['Not Found', 404] |
@@ -45,7 +45,7 @@ module Agents |
||
45 | 45 |
} |
46 | 46 |
end |
47 | 47 |
|
48 |
- def receive_web_request(params, method, format, headers={}) |
|
48 |
+ def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({})) |
|
49 | 49 |
# check the secret |
50 | 50 |
secret = params.delete('secret') |
51 | 51 |
return ["Not Authorized", 401] unless secret == interpolated['secret'] |
@@ -5,7 +5,7 @@ describe WebRequestsController do |
||
5 | 5 |
cannot_receive_events! |
6 | 6 |
cannot_be_scheduled! |
7 | 7 |
|
8 |
- def receive_web_request(params, method, format, headers={}) |
|
8 |
+ def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({})) |
|
9 | 9 |
if params.delete(:secret) == options[:secret] |
10 | 10 |
memory[:web_request_values] = params |
11 | 11 |
memory[:web_request_format] = format |
@@ -729,20 +729,20 @@ describe Agent do |
||
729 | 729 |
@agent.user = users(:bob) |
730 | 730 |
@agent.save! |
731 | 731 |
|
732 |
- def @agent.receive_web_request(params, method, format, headers={}) |
|
732 |
+ def @agent.receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({})) |
|
733 | 733 |
memory['last_request'] = [params, method, format, headers] |
734 | 734 |
['Ok!', 200] |
735 | 735 |
end |
736 | 736 |
end |
737 | 737 |
|
738 | 738 |
it "calls the .receive_web_request hook, updates last_web_request_at, and saves" do |
739 |
- @agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html", {}) |
|
739 |
+ @agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html", {}, ActionDispatch::Request.new({})) |
|
740 | 740 |
expect(@agent.reload.memory['last_request']).to eq([ { "some_param" => "some_value" }, "post", "text/html", {} ]) |
741 | 741 |
expect(@agent.last_web_request_at.to_i).to be_within(1).of(Time.now.to_i) |
742 | 742 |
end |
743 | 743 |
end |
744 | 744 |
|
745 |
- context "when .receive_web_request is defined without headers" do |
|
745 |
+ context "when .receive_web_request is defined without headers or request" do |
|
746 | 746 |
before do |
747 | 747 |
@agent = Agents::WebRequestReceiver.new(:name => "something") |
748 | 748 |
@agent.user = users(:bob) |
@@ -755,7 +755,7 @@ describe Agent do |
||
755 | 755 |
end |
756 | 756 |
|
757 | 757 |
it "calls the .trigger_web_request with headers, but they don't get passed to .receive_web_request" do |
758 |
- @agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html", {}) |
|
758 |
+ @agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html", {}, ActionDispatch::Request.new({})) |
|
759 | 759 |
expect(@agent.reload.memory['last_request']).to eq([ { "some_param" => "some_value" }, "post", "text/html" ]) |
760 | 760 |
expect(@agent.last_web_request_at.to_i).to be_within(1).of(Time.now.to_i) |
761 | 761 |
end |
@@ -775,7 +775,7 @@ describe Agent do |
||
775 | 775 |
|
776 | 776 |
it "outputs a deprecation warning and calls .receive_webhook with the params" do |
777 | 777 |
mock(Rails.logger).warn("DEPRECATED: The .receive_webhook method is deprecated, please switch your Agent to use .receive_web_request.") |
778 |
- @agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html", {}) |
|
778 |
+ @agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html", {}, ActionDispatch::Request.new({})) |
|
779 | 779 |
expect(@agent.reload.memory['last_webhook_request']).to eq({ "some_param" => "some_value" }) |
780 | 780 |
expect(@agent.last_web_request_at.to_i).to be_within(1).of(Time.now.to_i) |
781 | 781 |
end |